#!/bin/bash -eu

# Setup sandbox
cabal v1-sandbox init
cabal v1-install --only-dependencies
cabal v1-sandbox add-source ../source
cabal v1-install BNFC

# Check for tools required to run system tests
missing=""

# $1: Command name
# $2: Command line to test if $1 is available (default: `type $1`)
check() {
  echo -n "Checking for $1... "
  test=${2:-type $1}
  if ! $test &> /dev/null ; then
    missing+=" $1"
    echo -e "\033[33mmissing\033[0m"
  else
    echo -e "\033[32mOK\033[0m"
  fi
}

check bnfc
check gcc
check flex
check bison
check alex
check happy
check hlint
check javac
check jflex
check cup "javap java_cup.Main"
check antlr "javap org.antlr.v4.Tool"
check ocamlc
check ocamllex
check ocamlyacc
## TODO: add installation of these tools on travis
# check menhir
# check agda

if [ ! -z "$missing" ]; then
  >&2 echo -e "\033[31mSome tests will fail as they require tools that are missing from your system:\033[0m"
  >&2 echo -e "\033[31m$missing\033[0m"
  exit 1
fi
